package edu.unmsm.ocaa.sisocaa.negocio.service.impl;

import javax.annotation.Resource;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.User;
import org.springframework.stereotype.Service;

import edu.unmsm.ocaa.sisocaa.negocio.service.UserManagerService;
import edu.unmsm.ocaa.sisocaa.negocio.service.UsuarioService;


@Service
public class UserManagerServiceImpl implements UserManagerService {

	@Resource
	UsuarioService usuServ;
	

	public User getUser(String usuario, String pass){
		User user=null;
		try {
			Integer autoridad=usuServ.getUsuarioCorrecto(usuario, pass);
			GrantedAuthority[] grantedAuthorities;		
			GrantedAuthority authority ;
			
			if(autoridad!=-1){
				
				grantedAuthorities = new GrantedAuthority[1];
				String rol=null;
					//USUARIO SUELDOS
					if(autoridad==2){
						rol="ROLE_ADMIN";
					}else if (autoridad==1) {
						rol="ROLE_ADMIN_DEP";
					}else {
						rol="ROLE_USER";
					}
					authority = new GrantedAuthorityImpl(rol);
					grantedAuthorities[0] = authority;
				
				
				user=new User(usuario,pass,true, true,true, true, grantedAuthorities);
				
			}else if(autoridad==-1){
				grantedAuthorities = new GrantedAuthority[1];
				authority = new GrantedAuthorityImpl("IS_AUTHENTICATED_ANONYMOUSLY");
				grantedAuthorities[0] = authority;
				user=new User(usuario,pass,false, true,true,false, grantedAuthorities);		
				return user;
			}			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		return user;
				
		
	}
	
	
}
